﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Map_Creator.RedoUndo
{
    public partial class RedoUndoStrip : ToolStrip, IRedoUndo
    {
        public event RedoUndoDelegate OnRedoUndo;

        public RedoUndoStrip()
        {
            InitializeComponent();
        }

        public RedoUndoStrip(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        void Redo()
        {
            if (RedoButton.DropDownItems.Count == 0) return;
            var action = (IAction) RedoButton.DropDownItems[0].Tag;
            action.Do();
            // This also removes? O_o maybe it cant be in 2 places at the same time :D
            UndoButton.DropDownItems.Insert(0, RedoButton.DropDownItems[0]);
            
            var handler = OnRedoUndo;
            if (handler != null) handler(this, action);
        }

        void Undo()
        {
            if (UndoButton.DropDownItems.Count == 0) return;
            var action = (IAction) UndoButton.DropDownItems[0].Tag;
            action.Undo();
            // This also removes? O_o maybe it cant be in 2 places at the same time :D
            RedoButton.DropDownItems.Insert(0, UndoButton.DropDownItems[0]);
            
            var handler = OnRedoUndo;
            if (handler != null) handler(this, action);
        }


        public void AddActionAndExecute(IAction action)
        {
            action.Do();
            AddAction(action);
        }

        public void AddAction(IAction action)
        {
            var toolStripItem = new ToolStripButton(action.GetName());
            toolStripItem.Tag = action;
            toolStripItem.ToolTipText = action.GetDescription();
            toolStripItem.Click += Action_OnClick;
            UndoButton.DropDownItems.Insert(0, toolStripItem);
            // TODO: Change from static 25
            // this is for preformance... adding to a list too big will take a year
            if (UndoButton.DropDownItems.Count > 25)
                UndoButton.DropDownItems.RemoveAt(25);
            if (RedoButton.DropDownItems.Count > 0)
                RedoButton.DropDownItems.Clear();
        }

        private void Action_OnClick(object sender, EventArgs e)
        {
            var senderItem = (ToolStripItem) sender;
            if (senderItem.OwnerItem == UndoButton)
            {
                while (UndoButton.DropDownItems[0] != senderItem)
                    Undo();
                Undo();
            }
            else if (senderItem.OwnerItem == RedoButton)
            {
                while (RedoButton.DropDownItems[0] != senderItem)
                    Redo();
                Redo();
            }
        }

        private void OnRedoButtonClick(object sender, EventArgs e)
        {
            Redo();
        }

        private void OnUndoButtonClick(object sender, EventArgs e)
        {
            Undo();
        }
    }
}